Imports DotNetPM.Business.Questionnaire

Partial Class Modules_Questionnaires_Controls_ansMultipleChoicesSingleAnswer
    Inherits System.Web.UI.UserControl
    Implements DotNetPM.Business.Questionnaire.Taker.IAnswerUC

    Private _config, _answerType, _questionId, _sortId As String
    Private _controlType As Boolean
    Private rlChoices As RadioButtonList
    Private ddlChoices As DropDownList

    Public Property Answer() As String Implements DotNetPM.Business.Questionnaire.Taker.IAnswerUC.Answer
        Get
            Dim _ans As String = String.Empty
            If _controlType Then
                _ans = rlChoices.SelectedValue
            Else
                _ans = ddlChoices.SelectedValue
            End If
            Return _ans
        End Get
        Set(ByVal value As String)
            If String.IsNullOrEmpty(value) Then
                Exit Property
            End If
            If _controlType Then
                rlChoices.SelectedValue = value
            Else
                ddlChoices.SelectedValue = value
            End If
        End Set
    End Property

    Public Property Config() As String Implements DotNetPM.Business.Questionnaire.Taker.IAnswerUC.Config
        Get
            Return _config
        End Get
        Set(ByVal value As String)
            If Not String.IsNullOrEmpty(value) Then
                _config = value
            End If
        End Set
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        LoadAnswerControl()
        'Response.Write("Page_Load: " & Me.Config & "<br/>")
    End Sub

    Private Sub LoadAnswerControl()
        Dim val As String() = Split(Me.Config, "||")
        _sortId = val(0)
        _questionId = val(1)
        _answerType = val(2)
        _controlType = CBool(val(3))

        Dim _isRequired As String = val(4)
        Dim reqVal As New RequiredFieldValidator
        If _isRequired = "0" Then
            With reqVal
                .ID = "reqVal"
                .ControlToValidate = "ctrlChoices"
                .Display = ValidatorDisplay.Dynamic
                .Text = "*"
                .ErrorMessage = "Please select an answer!"
                '.ErrorMessage = "Question# <b>" & _questionId & "</b> : The answer of is required."
                '.Text = "*"
            End With
            phValidator.Controls.Add(reqVal)
        End If

        If _controlType Then
            ' Radio Button List
            rlChoices = New RadioButtonList
            phChoices.Controls.Add(rlChoices)
            With rlChoices
                .ID = "ctrlChoices"
                .RepeatColumns = val(5)
                .RepeatDirection = val(6)
                .RepeatLayout = val(7)
                .DataSource = QuestionODS.GetChoices(_questionId)
                .DataTextField = "Choice"
                .DataValueField = "SortID"
                .DataBind()
            End With
        Else
            ' DropDown List
            ddlChoices = New DropDownList
            phChoices.Controls.Add(ddlChoices)
            Dim li As New ListItem
            With ddlChoices
                .ID = "ctrlChoices"
                .Items.Clear()
                .Items.Add(li)
                .AppendDataBoundItems = True
                .DataSource = QuestionODS.GetChoices(_questionId)
                .DataTextField = "Choice"
                .DataValueField = "SortID"
                .DataBind()
            End With
            reqVal.Text = "*"
            reqVal.ErrorMessage = "Please select an answer!"
            'reqVal.ErrorMessage = "&nbsp;*<br>The answer cannot be blank."
        End If
    End Sub

End Class
